package com.hongye.controller;

import java.util.List;

import com.hongye.app.model.GoodsModel;
import com.hongye.app.model.OrderGoodsModel;
import com.hongye.app.model.OrderInfoModel;
import com.hongye.app.model.ReturnGoodsModel;
import com.hongye.util.AlipayUtils;
import com.hongye.util.PushUtils;
import com.hongye.util.Utils;
import com.hongye.util.WeixinUtils;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

/**
 * 买/卖双方处理退货的逻辑
 * @author liushaomin
 */
public class ReturnGoodsController extends Controller{
	
	public static final String BaseURL = "http://120.27.117.192/rbg/app/";
	public static final String BuyersURL = "buyersReturnDetail?returngoodsid=";
	public static final String SellerURL = "sellerReturnDetail?returngoodsid=";
	
	public ReturnGoodsController() {
		
	}
	
	/**
	 * 取消退货申请
	 * 取消退货申请后，订单状态修改为原状态（已发货）
	 * @author liushaomin
	 * 2016年11月23日
	 */
	@Clear
	public void cancelApply(){
		String id = getPara("id");
		ReturnGoodsModel reModel = ReturnGoodsModel.dao.findById(id);
		if(reModel.set("return_status", 3).set("update_time", Utils.getDate()).set("data", ReturnGoodsModel.getData(reModel, 3, "买家", "买家取消退货申请")).update()){
			OrderInfoModel order = OrderInfoModel.getOrderBySN(reModel.getStr("g_order"));
			if(order.set("order_status", 3).update()){
				reModel.set("data", ReturnGoodsModel.getData(reModel, -2, "热播购系统", "买家取消了退货申请")).update();
			}
		}
		this.redirect(BaseURL + BuyersURL + id);
	}

	/**
	 * 商家同意退货
	 * 修改order状态为退货中（10）
	 * @author liushaomin
	 * 2016年11月23日
	 */
	@Clear
	public void agreeApply(){
		String id = getPara("id");
		ReturnGoodsModel reModel = ReturnGoodsModel.dao.findById(id);
		if(reModel.set("return_status", 2).set("update_time", Utils.getDate()).set("data", ReturnGoodsModel.getData(reModel, 2, "商家", "商家同意退货申请")).update()){
			OrderInfoModel order = OrderInfoModel.getOrderBySN(reModel.getStr("g_order"));
			if(order.set("order_status", 10).update()){
				if(reModel.set("data", ReturnGoodsModel.getData(reModel, -3, "热播购系统", "商家已同意了退货申请")).update()){
					PushUtils.MSG_Single(order.getStr("phone"), "A05", "您的退货进度更新啦！去看看吧！");
				}
//				boolean boo_refund = false; 
//				if(upboo){
//					OrderInfoModel order_all = OrderInfoModel.isTotalOrder(order.get("id"));
//					if(order_all != null){//多个订单支付情况
//						if("0".equals(order_all.get("payment_channel"))){//支付宝退款
//							boo_refund = AlipayUtils.refund(order_all.getStr("order_sn"), order.get("total_price"));
//						}else if("1".equals(order_all.get("payment_channel"))){//微信退款
//							boo_refund = WeixinUtils.refund(order_all.getStr("order_sn"), order_all.getFloat("total_price"), order.getFloat("total_price"));
//						}
//					}else{//单个订单支付情况
//						if("0".equals(order.get("payment_channel"))){//支付宝退款
//							boo_refund = AlipayUtils.refund(order.getStr("order_sn"), order.get("total_price"));
//						}else if("1".equals(order.get("payment_channel"))){//微信退款
//							boo_refund = WeixinUtils.refund(order.getStr("order_sn"), order.getFloat("total_price"), order.getFloat("total_price"));
//						}
//					}
//				}
//				if(boo_refund){
//					reModel.set("return_status", 11).set("update_time", Utils.getDate()).set("data", ReturnGoodsModel.getData(reModel, -1, "热播购系统", "已退款成功")).update();
//					GoodsModel.dao.edit_stock_sales(reModel.getInt("g_id"), 1, reModel.getInt("g_num"));
//					PushUtils.MSG_Single(order.getStr("phone"), "A05", "您的退货进度更新啦！去看看吧！");
//				}else{
//					OrderInfoModel.UpdateOrderStatus(order, 10);
//				}
			}
		}
		this.redirect(BaseURL + SellerURL + id);
	}
	
	/**
	 * 商家确认收货
	 * 确认收货后 再给买家退款
	 * @author liushaomin
	 * 2016年11月23日
	 */
	@Clear
	public void confirm(){
		String id = getPara("id");
		ReturnGoodsModel reModel = ReturnGoodsModel.dao.findById(id);
		if(reModel.set("return_status", 7).set("update_time", Utils.getDate()).set("data", ReturnGoodsModel.getData(reModel, 7, "商家", "商家已收到货物")).update()){
			OrderInfoModel order = OrderInfoModel.getOrderBySN(reModel.getStr("g_order"));
			boolean boo = order.set("order_status", 11).update();
			boolean boo_refund = false; 
			if(boo){
				OrderInfoModel order_all = OrderInfoModel.isTotalOrder(order.get("id"));
				if(order_all != null){//多个订单支付情况
					if("0".equals(order_all.get("payment_channel"))){//支付宝退款
						boo_refund = AlipayUtils.refund(order_all.getStr("order_sn"), order.get("total_price"));
					}else if("1".equals(order_all.get("payment_channel"))){//微信退款
						boo_refund = WeixinUtils.refund(order_all.getStr("order_sn"), order_all.getFloat("total_price"), order.getFloat("total_price"));
					}
				}else{//单个订单支付情况
					if("0".equals(order.get("payment_channel"))){//支付宝退款
						boo_refund = AlipayUtils.refund(order.getStr("order_sn"), order.get("total_price"));
					}else if("1".equals(order.get("payment_channel"))){//微信退款
						boo_refund = WeixinUtils.refund(order.getStr("order_sn"), order.getFloat("total_price"), order.getFloat("total_price"));
					}
				}
			}
			if(boo_refund){
				reModel.set("update_time", Utils.getDate()).set("data", ReturnGoodsModel.getData(reModel, -6, "热播购系统", "订单金额已退回")).update();
				List<OrderGoodsModel> og = OrderGoodsModel.getByOrderId(order.getInt("id"));
				for (OrderGoodsModel item : og) {
					GoodsModel.dao.edit_stock_sales(item.getInt("goods_id"), 1, item.getInt("goods_num"));
				}
				reModel.set("data", ReturnGoodsModel.getData(reModel, -5, "热播购系统", "商家已收货，订单金额已退回，退货完成")).update();
				PushUtils.MSG_Single(order.getStr("phone"), "A05", "您的退货已完成！去看看吧！");
			}else{
				OrderInfoModel.UpdateOrderStatus(order, 10);//如果失败返回之前订单状态
			}
		}
		this.redirect(BaseURL + SellerURL + id);
	}
}
